package com.jwdf.uda.http.action;

import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.jwdf.uda.app.AppContext;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.ApiService;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.utils.NETUtils;

import java.util.Locale;

import retrofit2.Call;

/**
 * 类说明：更新设备的信息
 * 作者：huangqiuxin on 2016/6/15 10:10
 * 邮箱：648859026@qq.com
 */
public class UpdateDeviceInfo extends AHttpService<PbBean.PbResSystemUpdateDeviceInfo> {
    /**
     * 更新设备的信息
     *
     * @param context
     * @param pushToken
     * @return
     */
    public static UpdateDeviceInfo newInstance(Context context, String pushToken) {
        return new UpdateDeviceInfo(context, pushToken);
    }

    private int platform = 2;//平台 1苹果 2安卓
    private String os = "";//操作系统
    private String model = "";//型号 如小米
    private int screenWidth = 0;//屏幕宽度
    private int screenHeight = 0;//屏幕高度
    private String udid = "";//设备唯一标识
    private String idfa = "";
    private String imei = "";
    private String imsi = "";
    private int network = 1;//网络类型
    private String language = "";//语言
    private String country = "";//国家
    private int buildNum = 1;//数字版本号
    private String version = "";//文字版本号
    private String packagename = "";//包名
    private String channel = "";//发布渠道
    private String pushToken = "";//推送的Token

    private UpdateDeviceInfo(Context context, String pushToken) {
        super(context);
        os = android.os.Build.VERSION.RELEASE;
        model = Build.MODEL;
        screenWidth = DisplayUtils.getDeviceWidth(context);
        screenHeight = DisplayUtils.getDeviceHeight(context);
        udid = AppContext.getUDID(context);
        TelephonyManager mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        imsi = mTelephonyMgr.getSubscriberId() == null ? "" : imsi.replaceAll("[^\\da-zA-Z]*", "");
        imei = mTelephonyMgr.getDeviceId();
        network = NETUtils.getNetworkType(context);
        language = Locale.getDefault().getLanguage();
        country = Locale.getDefault().getCountry();
        buildNum = AppContext.getAppVersionCode(context);
        version = AppContext.getAppVersionName(context);
        packagename = AppContext.getAppPackageName(context);
        channel = AppContext.getChannel(context);
        this.pushToken = pushToken;
    }

    @Override
    protected Call newRetrofitCall(ApiService apiService, PbBean.PbBaseClientInfo.Builder baseBuilder) {
        PbBean.PbReqSystemUpdateDeviceInfo.Builder builder = PbBean.PbReqSystemUpdateDeviceInfo.newBuilder();
        builder.setBaseClientInfo(baseBuilder);
        builder.setPlatform(platform);
        builder.setOs(os);
        builder.setModel(model);
        builder.setScreenWidth(screenWidth);
        builder.setScreenHeight(screenHeight);
        builder.setUdid(udid);
        builder.setIdfa(idfa);
        builder.setImei(imei);
        builder.setImsi(imsi);
        builder.setNetwork(network);
        builder.setLanguage(language);
        builder.setCountry(country);
        builder.setBuildNum(buildNum);
        builder.setVersion(version);
        builder.setPackagename(packagename);
        builder.setChannel(channel);
        builder.setPushToken(pushToken);

        return apiService.updateDeviceInfo(builder.build());
    }
}
